#ifndef	FlexPWM_h
#define	FlexPWM_h

//  режим Fast-PWM

#include	<Arduino.h>

#define __bit(b) (1u << (b))

inline void pwmInit(const unsigned char bitRate = 16) {
	noInterrupts();
	TCCR1A = __bit(WGM11);
	TCCR1B = __bit(WGM13) | __bit(WGM12) | __bit(CS10);
	TCCR1C = TCNT1 = OCR1A = OCR1B = TIMSK1 = TIFR1 = 0;
	ICR1 = (1u << bitRate) - 1u;
	interrupts();
}

inline void pwmWrite9(const unsigned v) {
	DDRB |= __bit(PB1);
	TCCR1A |= __bit(COM1A1);
	OCR1A = v;
}

inline void digitalWrite9(const bool v) {
	DDRB |= __bit(PB1);
	TCCR1A &= ~__bit(COM1A1);
	if (v) PORTB |= __bit(PB1); else PORTB &= ~__bit(PB1);
}

inline void pwmWrite10(const unsigned v) {
	DDRB |= __bit(PB2);
	TCCR1A |= __bit(COM1B1);
	OCR1B = v;
}

inline void digitalWrite10(const bool v) {
	DDRB |= __bit(PB2);
	TCCR1A &= ~__bit(COM1B1);
	if (v) PORTB |= __bit(PB2); else PORTB &= ~__bit(PB2);
}

#endif	// FlexPWM_h
